<?php

namespace App\Http\ViewComposers;

use App\Handlers\role;
use Illuminate\View\View;
use App\Services\Config\ChannelsService;
use Auth;

class ServerComposer
{
    protected $channelService;

    public function __construct(ChannelsService $channelsService)
    {
        $this->channelsService = $channelsService;
    }

    public function compose(View $view)
    {
        $servers = Auth::guard('admin')->user()->getServers(['game_id' => session('game_id')], 'channel_id');
        $reqChannelId = $view->reqChannelId;
        $reqUniqueId = $view->reqUniqueId;

        //拼出 渠道table
        $str_arr_unique_id = '';
        $serverHtml = '';
        foreach ($servers as $server) {
            $uniqueID = $server['unique_id'];
            $channel_name = $server['channel_name'];
            $channel_id = $server['channel_id'];
            $server_name = $server['server_name'];
            $checked = '';
            $show = 'display:none;';

            if (empty($reqChannelId) || in_array($server['channel_id'], $reqChannelId)) {
                $show = 'display:inline-block;';
            }

            if (empty($reqUniqueId) || in_array($uniqueID, $reqUniqueId)) {
                $checked = 'checked="checked"';
                $str_arr_unique_id .= $uniqueID . ',';
            }

            $serverHtml .= <<<EOF
 <div class="col-sm-2" style="min-width: 20rem;$show" data-channel-id="$channel_id">
     <label>&emsp;$channel_name($server_name)
            <input class="pull-left" type="checkbox" name="unique_id[]" $checked value="$uniqueID">
    </label>
</div>
EOF;
        }

        $str_arr_unique_id = (empty($str_arr_unique_id) ? '' : substr($str_arr_unique_id, 0, -1));

        $view->with(compact('str_arr_unique_id', 'serverHtml'));
    }
}